package com.zzyl.serve.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import com.zzyl.serve.domain.DeviceData;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.security.core.parameters.P;

/**
 * 设备数据Mapper接口
 * 
 * @author ruoyi
 * @date 2025-03-15
 */
@Mapper
public interface DeviceDataMapper extends BaseMapper<DeviceData>
{
    /**
     * 查询设备数据
     * 
     * @param id 设备数据主键
     * @return 设备数据
     */
    public DeviceData selectDeviceDataById(Long id);

    /**
     * 查询设备数据列表
     * 
     * @param deviceData 设备数据
     * @return 设备数据集合
     */
    public List<DeviceData> selectDeviceDataList(DeviceData deviceData);

    /**
     * 新增设备数据
     * 
     * @param deviceData 设备数据
     * @return 结果
     */
    public int insertDeviceData(DeviceData deviceData);

    /**
     * 修改设备数据
     * 
     * @param deviceData 设备数据
     * @return 结果
     */
    public int updateDeviceData(DeviceData deviceData);

    /**
     * 删除设备数据
     * 
     * @param id 设备数据主键
     * @return 结果
     */
    public int deleteDeviceDataById(Long id);

    /**
     * 批量删除设备数据
     * 
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteDeviceDataByIds(Long[] ids);


    List<DeviceData> queryDeviceDataListByWeek(String elderName);


    /*@Select("\tSELECT \n" +
            "    DATE_FORMAT(alarm_time, '%H:00') AS dateTime,\n" +
            "    FLOOR(AVG(data_value)) AS dataValue\n" +
            "FROM \n" +
            "    device_data\n" +
            "WHERE \n" +
            "    function_id = #{functionId} \n" +
            "    AND iot_id = #{iotId}\n" +
            "    AND alarm_time BETWEEN #{startTime} AND #{endTime}\n" +
            "GROUP BY \n" +
            "    DATE_FORMAT(alarm_time, '%Y-%m-%d %H:00')\n" +
            "ORDER BY \n" +
            "    dateTime;")*/
    @Select("\tSELECT \n" +
            "    DATE_FORMAT(alarm_time, '%H:00') AS dateTime,\n" +
            "    FLOOR(AVG(data_value)) AS dataValue\n" +
            "FROM \n" +
            "    device_data\n" +
            "WHERE \n" +
            "    function_id = #{functionId} \n" +
            "    AND iot_id = #{iotId}\n" +
            "    AND alarm_time BETWEEN #{start} AND #{end}\n" +
            "GROUP BY \n" +
            "    DATE_FORMAT(alarm_time, '%Y-%m-%d %H:00')\n" +
            "ORDER BY \n" +
            "    dateTime;")
    List<Map<String, Object>> queryDeviceDataListByDay(@Param("functionId") String functionId,@Param("start") LocalDateTime start,@Param("end") LocalDateTime end,@Param("iotId") String iotId);


}
